<template>
  <div class='CategoryBr'>
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>{{$route.params.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 轮播图 -->
      <XtSlider :sliders="bannerList" style="height:500px" />
    </div>
    <!-- 所有二级分类 -->
    <div class="sub-list">
      <h3>全部分类</h3>
      <ul>
        <li v-for="item in cateList" :key="item.id">
          <!-- <a href="javascript:;">
            <img :src="item.picture">
            <p>{{item.name}}</p>
          </a> -->
          <!-- 更改商品跳转路由参数 -->
          <router-link :to="`/goods/${item.id}`">
             <img :src="item.picture">
            <p>{{item.name}}</p>
          </router-link>
        </li>
      </ul>
    </div>
    <!-- 分类关联商品 -->
    <div class="ref-goods" v-for="item in cateList" :key="item">
      <!-- 标题 -->
      <div class="head">
        <h3>- {{item.name}} -</h3>
        <XtxMore to="/" />
      </div>
      <!-- 商品主体 -->
      <div class="body">
        <RouterLink :to="'/goods'+ goods.id" class="goods-item" v-for="good in item.goods" :key="good.id">
          <img :src="good.picture" alt="" />
          <p class="name ellipsis">{{good.name}}</p>
          <p class="desc ellipsis">{{good.desc}}</p>
          <p class="price">&yen;{{good.price}}</p>
        </RouterLink>
      </div>
    </div>
  </div>
</template>

<script>
import { homeBanner } from '@/api/home/index.js'
import { ref } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import { categoryDetail } from '@/api/catagory/index.js'
export default {
  name: 'CategoryBr',
  setup () {
    // 设置初始相应数据
    const bannerList = ref([])
    // 设置二级分类初始值
    const cateList = ref([])
    const route = useRoute()
    // 方案三===> 路由缓存解决方案三:onBeforeUpdateRoute钩子函数
    // to:表示目标路由
    onBeforeRouteUpdate(to => {
      async function loadHomeBannerList () {
        const res = await homeBanner()
        console.log(res, '轮播图数据1111')
        bannerList.value = res.result
      }
      loadHomeBannerList()
      categoryDetail(route.params.id).then(res => {
        cateList.value = res.result.children
        console.log(res, '分类详情')
      })
    })

    // 2：发送ajax请求数据
    // 监听路由参数的变化,变化之后就重新执行数据请求和赋值,解决因为路由缓存导致的组件复用
    // watch(() => route.params.id, (newVal) => {
    //   async function loadHomeBannerList () {
    //     const res = await homeBanner()
    //     console.log(res, '轮播图数据1111')
    //     bannerList.value = res.result
    //   }
    //   // 3：调用方法
    //   loadHomeBannerList()
    //   // // 设置二级分类初始值
    //   // const cateList = ref([])
    //   // const route = useRoute()
    //   // 设置全部分类的商品列表中商品初始值
    //   // const goodsList = ref([])/
    //   categoryDetail(route.params.id).then(res => {
    //     cateList.value = res.result.children
    //     console.log(res, '分类详情')
    //   })
    // }, { immediate: true })
    // async function loadHomeBannerList () {
    //   const res = await homeBanner()
    //   console.log(res, '轮播图数据1111')
    //   bannerList.value = res.result
    // }
    // // 3：调用方法
    // loadHomeBannerList()
    // // // 设置二级分类初始值
    // // const cateList = ref([])
    // // const route = useRoute()
    // // 设置全部分类的商品列表中商品初始值
    // // const goodsList = ref([])/
    // categoryDetail(route.params.id).then(res => {
    //   cateList.value = res.result.children
    //   console.log(res, '分类详情')
    // })
    return {
      bannerList,
      cateList
    }
  }
}
</script>
<style scoped lang="less">
.CategoryBr {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      padding: 0 65px 30px;
      flex-wrap: wrap;
    }
  }
}
.goods-item {
  display: block;
  width: 220px;
  padding: 20px 30px;
  text-align: center;
  img {
    width: 160px;
    height: 160px;
  }
  p {
    padding-top: 10px;
  }
  .name {
    font-size: 16px;
  }
  .desc {
    color: #999;
    height: 29px;
  }
  .price {
    color: @priceColor;
    font-size: 20px;
  }
}
</style>
